package com.tz.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.tz.bean.TzParams;
import com.tz.bean.User;
import com.tz.service.user.IUserService;

@Controller
@RequestMapping("/admin/user")
public class UserController {
	
	@Autowired
	private IUserService userService;
	
	@RequestMapping("/list")
	public String userList(TzParams params){
		return "user/list";
	}
	
	@RequestMapping("/template")
	public ModelAndView template(TzParams params){
		ModelAndView modelAndView = new ModelAndView();
		List<User> users = userService.findUsers(params);
		int count = userService.count(params);
		modelAndView.setViewName("/user/template");
		modelAndView.addObject("users",users);
		modelAndView.addObject("count",count);
		return modelAndView;
	}
	
	@RequestMapping("/detaillist")
	public String userDetaillist(TzParams params){
		return "user/detaillist";
	}
	
	@RequestMapping("/detail")
	public ModelAndView detaillist(TzParams params){
		ModelAndView modelAndView = new ModelAndView();
		List<User> usersdetails = userService.findDetail(params);
		modelAndView.setViewName("/user/detail");
		modelAndView.addObject("usersdetails",usersdetails);
		return modelAndView;
	}
	
	@ResponseBody
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public String delete(TzParams params){
		userService.delete(params);
		return "success";
	}
	
	@ResponseBody
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public String update(TzParams params){
		userService.update(params);
		return "success";
	}
	
}
